﻿using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Entities.Data;

public class scriptConnection : MonoBehaviour {
    private string ServerIP = "127.0.0.1";
    private string ServerIP1 = "128.199.103.138";
    public int ServerPort = 9933;
    public string Zone = "BasicExamples";
    public GUISkin sfsSkin;
    private string username;
    private string loginErrorMessage;
    private  SmartFox sfs;

    void Awake()
    {
        Application.runInBackground = true;
    }

	// Use this for initialization
	void Start () 
    {
        sfs = new SmartFox();
        sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
        sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
        sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
        sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
        sfs.AddEventListener(SFSEvent.UDP_INIT, OnUdpInit);
        sfs.Connect(ServerIP1, ServerPort);
	}

    private void OnLoginError(BaseEvent evt)
    {
        loginErrorMessage = (string)evt.Params["errorMessage"];
        Debug.Log("UDP error: " + loginErrorMessage);
    }

    private void OnUdpInit(BaseEvent evt)
    {
        if (!(bool)evt.Params["success"])
        {
            loginErrorMessage = (string)evt.Params["errorMessage"];
            Debug.Log("UDP error: " + loginErrorMessage);
        }
        else
        {
            Debug.Log("UDP ok");
            loginErrorMessage = "";
            sfs.RemoveAllEventListeners();
            print("SUCCESSFUL");
        }
    }

    private void OnLogin(BaseEvent evt)
    {
        bool success = true;
        if (evt.Params.Contains("success") && !(bool)evt.Params["success"])
        {
            loginErrorMessage = (string)evt.Params["errorMessage"];
            Debug.Log("Login error: " + loginErrorMessage);
        }
        else
        {
            Debug.Log("Login ok");
            sfs.InitUDP(ServerIP1, ServerPort);
        }
    }

    private void OnConnectionLost(BaseEvent evt)
    {
        if (sfs.IsConnected)
        {
            sfs.RemoveAllEventListeners();
        }
        
    }

    private void OnConnection(BaseEvent evt)
    {
        bool success = (bool)evt.Params["success"];
        string error = (string)evt.Params["error"];
        if (success)
        {
            print("Connected");
        }
        else
        {
            print(error);
        }
    }
	
	// Update is called once per frame
	void FixedUpdate () 
    {
        sfs.ProcessEvents();
	}

    SFSObject isfs = new SFSObject();
    
    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 200, 50), "Login"))
        {
            Debug.Log("Sending login request");
            isfs.PutUtfString("fb_userid", "123456");
            isfs.PutUtfString("name", "hoangwy");
            isfs.PutUtfString("first_name", "quy");
            isfs.PutUtfString("last_name", "quy");
            isfs.PutUtfString("avatar", "quy");
            isfs.PutUtfString("email", "hoangwy@gmail.com");
            isfs.PutUtfString("access_token", "quy");
            sfs.Send(new LoginRequest("User01", "", Zone, isfs));
        }
    }
}
